package com.xiaoma.verbal.webservice;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.aspectj.apache.bcel.classfile.Utility.ResultHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.service.UserService;
import com.xiaoma.tpo.utils.TPOUtils;
import com.xiaoma.verbal.domain.PrctPracticeAudio;
import com.xiaoma.verbal.dto.CorrectedAudioVo;
import com.xiaoma.verbal.service.PrctPracticeAudioService;
import com.xiaoma.verbal.utils.HandleResultUtils;
import com.xiaoma.verbal.utils.Pagination;

@Controller
@RequestMapping("/service/practice/audio")
public class PrctPracticeAudioWS {
	@Autowired
	private PrctPracticeAudioService prctPracticeAudioService;
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> save(@RequestHeader String token, @RequestBody PrctPracticeAudio audio, HttpServletRequest request, ModelMap model,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			User user = null;
			if ((user = this.validateUser(token, result)) == null) {
				return result;
			}
			if (prctPracticeAudioService.getByUuid(user.getId(), audio.getUuid()) != null) {
				result.put("status", 0);
				result.put("tips", "已做过此题！");
				return result;
			}
			audio.setUserId(user.getId());
			audio = this.prctPracticeAudioService.save(audio);
			this.prctPracticeAudioService.sendToAssistantPerformance(audio);
			result.put("content", audio);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}

	@RequestMapping(value = "/uuid", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getByUuid(@RequestHeader String token, String uuid, HttpServletRequest request, ModelMap model,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			User user = null;
			if ((user = this.validateUser(token, result)) == null) {
				return result;
			}
			CorrectedAudioVo audio = this.prctPracticeAudioService.getByUuid(user.getId(), uuid);
			result.put("content", audio);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(@RequestHeader String token, String questionNum, String uuid, Integer status, Integer pageNo, Integer pageSize, HttpServletRequest request,
			ModelMap model,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			User user = null;
			if ((user = this.validateUser(token, result)) == null) {
				return result;
			}
			Pagination page = this.prctPracticeAudioService.list(user.getId(), questionNum, uuid, status, pageNo, pageSize);
			result.put("content", page);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) { 
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}

	private User validateUser(String token, Map<String, Object> result) {
		User user = userService.findByToken(token);
		if (null == user) {
			result.put("status", 0);
			result.put("tips", "invalid token");
			return null;
		}
		if (TPOUtils.over7days(new Date(), user.getLastVisit())) {// 大于7天token失效
			result.put("status", 0);
			result.put("tips", "token disabled over 7 days, please login");
			return null;
		}
		return user;
	}
	/**
	 * 
	 *新的 
	 * 
	 */
	//新的
	@RequestMapping(value = "/savenew", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveNew(@RequestHeader String token, @RequestBody PrctPracticeAudio audio, HttpServletRequest request, ModelMap model,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			User user = null;
			if ((user = this.validateUser(token, result)) == null) {
				return result;
			}
//			if (prctPracticeAudioService.getByUuid(user.getId(), audio.getUuid()) != null) {
//				result.put("status", 0);
//				result.put("tips", "已做过此题！");
//				return result;
//			}
//			if (prctPracticeAudioService.findByUserIdAndQuestionNumAndClassId(user.getId(),audio.getQuestionNum(),audio.getClsClassId())) {
//				
//				result.put("status", 0);
//				result.put("tips", "已做过此题！");
//				return result;
//			}
			audio.setUserId(user.getId());
			audio = this.prctPracticeAudioService.save(audio);
			this.prctPracticeAudioService.sendToAssistantPerformance(audio);
			result.put("content", audio);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}
	//新的
	@RequestMapping(value = "/listnew", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listNew(@RequestHeader String token, String questionNum, String uuid, Integer status, Integer pageNo, Integer pageSize, HttpServletRequest request,
			ModelMap model,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			User user = null;
			if ((user = this.validateUser(token, result)) == null) {
				return result;
			}
			Pagination page = this.prctPracticeAudioService.list(user.getId(), questionNum, uuid, status, pageNo, pageSize);
			result.put("content", page);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}
	//新的
	@RequestMapping(value = "/uuidnew", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getByUuidNew(@RequestHeader String token, String uuid, HttpServletRequest request, ModelMap model,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			User user = null;
			if ((user = this.validateUser(token, result)) == null) {
				return result;
			}
			CorrectedAudioVo audio = this.prctPracticeAudioService.getByUuidNew(user.getId(), uuid);
			result.put("content", audio);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}
}
